<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;

class Index extends Frontend {

    public function index() {
//        var_dump($this->request);die;
        $cate_id = $this->request->param('cate_id', 0);


        if (empty($this->userBoxId) || $this->userBoxId == 0) {
            $to_url = Session('to_url') ? Session('to_url') : url('My/index');
            $this->redirect($to_url);
        }

        //TODO 获取当前小区内的商品
//        $goods_ids = db('user_box_goods')->where(['user_box_id' => $this->userBoxId, 'is_del' => 0])->column('goods_id');
        //TODO 获取当前小区内的商品
        $user_box_info = db('user_box')
                ->where(['user_box_id' => $this->userBoxId, 'status' => 1])
                ->find();

        $goods_ids = db('house_goods')
                ->alias('hg')
                ->where(['hg.house_id' => $user_box_info['quarters_id'], 'hg.is_del' => 0])
                ->column('goods_id');

        $cate_ids = db('goods')->where('goods_id', 'in', $goods_ids)->column('cate_id');

        //TODO 获取当前小区内商品的类型
        $cate_list = db('goods_cate')->field('cate_id,cate_title')->where(['status' => 1, 'is_deleted' => 0, 'cate_id' => ['in', $cate_ids]])->order('sort desc')->select();

        
        $gwhere ='g.is_deleted = 0 and hg.house_id = '.$user_box_info['quarters_id'];
//        $gwhere['g.is_deleted'] = 0;
////        $gwhere['g.status'] = 1;
////        $gwhere['ubg.stock_num'] = ['gt', 0];
////        $gwhere['ubg.is_del'] = 0;
////        $gwhere['ubg.user_box_id'] = $this->userBoxId;
//        $gwhere['hg.house_id'] = $user_box_info['quarters_id'];

        $user_box_goods_ids = db('user_box_goods')->where('stock_num', '>', 0)->where('user_box_id', $this->userBoxId)->column('goods_id');
        
        if($user_box_goods_ids){
            $gwhere.=' and ( g.status=1 or g.goods_id in ('.implode(',', $user_box_goods_ids).'))';
        }
//        $gwhere['g.goods_id'] = ['not in', $user_box_goods_ids];
        $i = 0;
        foreach ($cate_list as &$row) {
//            $gwhere.=' and g.cate_id ='.$row['cate_id'];
//            $gotherwhere['g.cate_id'] = $row['cate_id'];
            $row['index_num'] = $i;
//            $goods_list = db('user_box_goods')
//                    ->alias('ubg')
//                    ->field('ubg.box_goods_id,g.goods_id,gb.brand_title,g.goods_title,g.goods_model,g.goods_spec,g.goods_desc,g.goods_logo,g.market_price,ubg.selling_price')
//                    ->join('fa_goods g', 'g.goods_id = ubg.goods_id')
//                    ->join('fa_goods_brand gb', 'g.brand_id = gb.brand_id')
//                    ->where($gwhere)
//                    ->order('g.sort')
//                    ->select();
            $goods_list = db('house_goods')
                    ->alias('hg')
                    ->field('g.goods_id,gb.brand_title,g.goods_title,g.goods_desc,g.goods_logo,g.goods_model,g.goods_spec,hg.selling_price')
                    ->join('fa_goods g', 'hg.goods_id=g.goods_id')
                    ->join('fa_goods_brand gb', 'g.brand_id = gb.brand_id')
                    ->where($gwhere)
                    ->where('g.cate_id',$row['cate_id'])
                    ->order('g.sort')
                    ->select();
//            echo db()->getLastSql();die;

            if ($goods_list) {
                //获取用户购物车数量
                foreach ($goods_list as $key=> &$val) {
//                    if ($val['status'] != 1) {
//                        if (!in_array($val['goods_id'], $user_box_goods_ids)) {
//                            unset($goods_list[$key]);
//                            continue;
//                        }
//                    }

                    $box_goods_info = db('user_box_goods')->where('goods_id', $val['goods_id'])->where('user_box_id', $this->userBoxId)->find();
                    if ($box_goods_info) {
                        $val['buy_num'] = getCardNumByGoods($this->iUserId, $box_goods_info['box_goods_id']);
                    } else {
                        $val['buy_num'] = 0;
                    }
                }
                $row['goods_list'] = $goods_list;
            } else {
                $row['goods_list'] = [];
            }
            $i++;
        }
        unset($row);

        //购物车数量及价格
        $cart = getCartTotalByUserId($this->iUserId, $this->userBoxId);


        //获取banner列表
        $banner_list = db('ad')
                ->alias('a')
                ->join('fa_ad_item ai', 'a.ad_id = ai.ad_id AND ai.position = 1', 'inner')
                ->where(['a.status' => 1, 'type' => 1])
                ->select();


        //文字广告
        $banner_text_list = db('ad')
                ->where(['postion_id' => 1, 'type' => 2])
                ->select();


        $this->assign('cart', $cart);
        $this->assign('last_cate', $i - 1);
        $this->assign('cate_id', $cate_id);
        $this->assign('cate_list', $cate_list);
        $this->assign('banner_list', $banner_list);
        $this->assign('banner_text_list', $banner_text_list);
        return $this->view->fetch();
    }

    public function cart() {

        $list = db('cart')
                ->alias('c')
                ->field('ubg.box_goods_id,g.goods_id,gb.brand_title,g.goods_title,g.goods_model,g.goods_desc,g.goods_logo,g.goods_spec,g.market_price,ubg.selling_price')
                ->join('fa_user_box_goods ubg', 'ubg.box_goods_id = c.box_goods_id')
                ->join('fa_goods g', 'ubg.goods_id=g.goods_id')
                ->join('fa_goods_brand gb', 'g.brand_id = gb.brand_id')
                ->where(['c.user_id' => $this->iUserId, 'c.user_box_id' => $this->userBoxId, 'c.type' => 0, 'ubg.is_del' => 0])
                ->order('c.id desc')
                ->select();


        if ($list) {
            foreach ($list as &$val) {
                $val['buy_num'] = getCardNumByGoods($this->iUserId, $val['box_goods_id']);
            }
        }
        //购物车数量及价格
        $cart = getCartTotalByUserId($this->iUserId, $this->userBoxId);

        $this->assign('list', $list);
        $this->assign('cart', $cart);
        return $this->view->fetch();
    }

    public function item() {
        $cate_id = $this->request->param('cate_id', 0);
        $p = $this->request->param('p', 0);
        $size = $this->request->param('psize', 6);
        $page = 0;
        if ($p) {
            $page = $p * $size;
        }

        $gwhere['g.is_deleted'] = 0;
        $gwhere['g.status'] = 1;
        $gwhere['ubg.stock_num'] = ['gt', 0];
        $gwhere['ubg.is_del'] = 0;
        if ($cate_id) {
            $gwhere['g.cate_id'] = $cate_id;
        }

        $gwhere['ubg.user_box_id'] = $this->userBoxId;

        $goods_list = db('user_box_goods')
                ->alias('ubg')
                ->field('ubg.box_goods_id,g.goods_id,gb.brand_title,g.goods_title,g.goods_model,g.goods_spec,g.goods_desc,g.goods_logo,g.market_price,ubg.selling_price')
                ->join('fa_goods g', 'g.goods_id = ubg.goods_id')
                ->join('fa_goods_brand gb', 'g.brand_id = gb.brand_id')
                ->where($gwhere)
                ->limit("$page,$size")
                ->order('g.createtime')
                ->select();

        if ($goods_list) {
            //获取用户购物车数量
            foreach ($goods_list as &$val) {
                $val['buy_num'] = getCardNumByGoods($this->iUserId, $val['box_goods_id']);
            }
        }
        $this->assign('goods_list', $goods_list);
        return $this->view->fetch();
    }

    public function grzx() {
        Session('user_box_id', 0);
        Session('to_url', url('My/index'));
        $this->redirect(url('My/index'));
    }

    public function sqmh() {
        Session('user_box_id', 0);
        Session('to_url', url('Box/apply'));
        $this->redirect(url('Box/apply'));
    }

}
